package cn.smbms.interceptor;

import cn.smbms.pojo.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: laoyu
 * @Date: 2019/9/6 11:47
 * @Description:
 */
public class SysInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //根据session记录决定用户是否有权限访问特定的页面
        User user = (User) request.getSession().getAttribute("user");
        //如果该用户已登录，则放行，允许访问所有页面，
        //反之，则重定向到401.jsp
        if (null==user) {
            response.sendRedirect(request.getContextPath()+"/401.jsp");
            return false;
        }
        return true;
    }
}
